package ca.uwindsor.cs.deepweb.utility.gov2;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;

import ca.uwindsor.cs.deepweb.utility.FileSizeDistributionAnalyzer;
import ca.uwindsor.cs.deepweb.utility.NekoHTMLHandler;

public class GOV2ContentSizeDistributionAnalyzer extends
		FileSizeDistributionAnalyzer {

	protected int indexnum;
	protected int idendswith;

	public GOV2ContentSizeDistributionAnalyzer(int size, String dir,
			int indexnum, int indends) {
		super(size, dir);
		// TODO Auto-generated constructor stub
		this.indexnum = indexnum;
		idendswith = indends;
	}

	protected void calculate(File dir) {
		File[] dataFiles = dir.listFiles();
		for (int i = 0; i < dataFiles.length; i++) {
			File file = dataFiles[i];
			if (file.isFile()) {
				if (isValidExtension(file.getName())) {
					long id = getFileNumericID(file);
					int index = new Long(id % indexnum).intValue();
					if (idendswith != -1 && index != idendswith) {
						continue;
					} else {
						// Long size = new Long(file.length());
						// addSize(size.intValue());
						NekoHTMLHandler handler = new NekoHTMLHandler("latin1");
						try {
							handler.processContent(new FileInputStream(
									file));
						} catch (Exception e) {
							// e.printStackTrace();
							// System.err.println("Error on parsing: "
							// + file.getCanonicalPath());
							continue;
						}

						String content = handler.getContent().replaceAll(
								"\\s+", " ");
						addSize(content.length());
					}
					// if (content.length() < 2048) {
					// continue;
					// }
				}
			} else if (file.isDirectory()) {
				calculate(file);
			}
		}
	}

	protected long getFileNumericID(File file) {
		long id = Long.valueOf(file.getName().replaceAll("[^\\d]", ""));
		return id;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// for testing only
//		args = new String[5];
//		args[0] = "--SourceDir=G:\\gov2-corpus-extracted\\GX000\\00";
//		args[1] = "--extension=html";
//		args[2] = "--intervalsize=500";
//		args[3] = "--idendswith=0";
//		args[4] = "--IndexCount=11";
		//
		String SourceDir = new String();
		// int size = Integer.parseInt(args[0]);
		String[] extensions = new String[] { "html" };
		int indexnum = 1;
		int idenswith = -1;
		int intervalsize = 1;

		if (args.length != 5) {
			System.out
					.println("usage: java -jar GOV2ContentSizeDistributionAnalyzer --SourceDir=sourcefiledirectory --extension=txt,html,... --intervalsize=size --IndexCount=count --idendswith=idsuffix");
			System.exit(0);
		} else {
			for (int i = 0; i < args.length; i++) {
				if (args[i].startsWith("--SourceDir=")) {
					SourceDir = args[i].substring(12);
				} else if (args[i].startsWith("--IndexCount=")) {
					indexnum = Integer.parseInt(args[i].substring(13));
				} else if (args[i].startsWith("--intervalsize=")) {
					intervalsize = Integer.parseInt(args[i].substring(15));
				} else if (args[i].startsWith("--extension=")) {
					String tempexts = args[i].substring(12);
					extensions = tempexts.split(",");
				} else if (args[i].startsWith("--idendswith=")) {
					idenswith = Integer.parseInt(args[i].substring(13));
				} else {
					System.out
							.println("usage: java -jar GOV2ContentSizeDistributionAnalyzer --SourceDir=sourcefiledirectory --extension=txt,html,... --intervalsize=size --idendswith=idsuffix");
					System.exit(0);
				}
			}
		}

		GOV2ContentSizeDistributionAnalyzer analyzer = new GOV2ContentSizeDistributionAnalyzer(
				intervalsize, SourceDir, indexnum, idenswith);
		for (int index = 0; index < extensions.length; index++) {
			analyzer.addExtension(extensions[index]);
		}
		System.out.println("Start!");
		try {
			analyzer.go();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ArrayList<Integer> distribution = analyzer.getDistribution();
		for (Integer value : distribution) {
			System.out.println(value);
		}
		System.out.println("finished!");
	}

}
